package gui.main.form;

import gui.listeners.MainFrameWindowAdapter;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import database.DBConnection;

public class LoginDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JTextField tfUsername;
	private JPasswordField pfPassword;
	private JLabel lbUsername;
	private JLabel lbPassword;
	private JButton btnLogin;
	private JButton btnCancel;
	private JFrame parent;
	private boolean succeeded;

	public LoginDialog(Frame parent) {
		super(parent, "Prijava na sistem", true);
		//
		this.parent = (JFrame) parent;
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints cs = new GridBagConstraints();

		cs.fill = GridBagConstraints.HORIZONTAL;

		lbUsername = new JLabel("Korisnicko ime: ");
		cs.gridx = 0;
		cs.gridy = 0;
		cs.gridwidth = 1;
		panel.add(lbUsername, cs);

		tfUsername = new JTextField(20);
		tfUsername.setText("admin");
		cs.gridx = 1;
		cs.gridy = 0;
		cs.gridwidth = 2;
		panel.add(tfUsername, cs);

		lbPassword = new JLabel("Lozinka: ");
		cs.gridx = 0;
		cs.gridy = 1;
		cs.gridwidth = 1;
		panel.add(lbPassword, cs);

		pfPassword = new JPasswordField(20);
		pfPassword.setText("12345");
		cs.gridx = 1;
		cs.gridy = 1;
		cs.gridwidth = 2;
		panel.add(pfPassword, cs);
		panel.setBorder(new LineBorder(Color.GRAY));

		btnLogin = new JButton("Prijavi se");

		this.getRootPane().setDefaultButton(btnLogin);

		btnLogin.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Properties properties = new Properties();
				try {
					properties.load(new FileInputStream(
							"users/users.properties"));
				} catch (IOException ex) {
					System.out.println("Users file not found.");
					return;
				}

				HashMap<String, String> users = new HashMap<String, String>();

				for (String key : properties.stringPropertyNames()) {
					if (key.substring(0, 4).equals("user")) {

						String user = properties.getProperty(key);
						String pass = properties.getProperty("pass"
								+ key.substring(4));
						if (user != null && pass != null) {
							user.toLowerCase();
							users.put(user, pass);
						}
					}
				}

				if (users.keySet().contains(
						tfUsername.getText().trim().toLowerCase())
						&& users.get(tfUsername.getText().trim().toLowerCase())
								.equals(new String(pfPassword.getPassword()))) {
					LoginDialog.this.parent.setTitle(LoginDialog.this.parent
							.getTitle()
							+ " (Prijavljen kao: "
							+ tfUsername.getText().trim() + ")");
					LoginDialog.this.setVisible(false);
				} else {
					JOptionPane.showMessageDialog(LoginDialog.this,
							"Neispravno korisnicko ime ili lozinka", "Prijavljivanje neuspesno",
							JOptionPane.ERROR_MESSAGE);
					// reset username and password
					tfUsername.setText("");
					pfPassword.setText("");
				}

			}
		});
		btnCancel = new JButton("Odustani");
		btnCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				DBConnection.close();
				System.exit(0);
			}
		});
		JPanel bp = new JPanel();
		bp.add(btnLogin);
		bp.add(btnCancel);

		getContentPane().add(panel, BorderLayout.CENTER);
		getContentPane().add(bp, BorderLayout.PAGE_END);

		pack();
		setResizable(false);
		setLocationRelativeTo(parent);

		addWindowListener(new MainFrameWindowAdapter());
	}

	public String getUsername() {
		return tfUsername.getText().trim();
	}

	public String getPassword() {
		return new String(pfPassword.getPassword());
	}

	public boolean isSucceeded() {
		return succeeded;
	}
}
